package com.example.node.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.node.R;
import com.example.node.adapter.ViewPagerAdapter;
import com.example.node.entity.Constant;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.SelectModel;
import com.example.node.ui.WebViewActivity;
import com.example.node.utils.GlideUtils;
import com.example.node.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SelectFragment extends Fragment {

    private ViewPager mSelectViewpager;
    private List<SelectModel> mList = new ArrayList<>();
    private List<View> mListPagerView = new ArrayList<>();
    private TextView tv_now_count;
    private TextView tv_all_count;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mSelectViewpager = view.findViewById(R.id.mSelectViewpager);
        tv_now_count = view.findViewById(R.id.tv_now_count);
        tv_all_count = view.findViewById(R.id.tv_all_count);

        initSelectData();
        initListener();
    }

    //初始化监听
    private void initListener() {
        mSelectViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setCount(i+1,mListPagerView.size());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    //初始化精选的数据
    private void initSelectData() {
        OkHttpUtils.get(Constant.HTTP_SELECT_URL, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    initViewPager(json);
                }
            }
        });
    }

    //初始化分页
    private void initViewPager(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonData = (JSONObject) jsonArray.get(i);

                String hp_title = jsonData.getString("hp_title");
                String hp_author = jsonData.getString("hp_author");
                String hp_content = jsonData.getString("hp_content");
                String last_update_date = jsonData.getString("last_update_date");
                String hp_img_url = jsonData.getString("hp_img_url");
                String web_url = jsonData.getString("web_url");
                String text_authors=jsonData.getString("text_authors");

                SelectModel selectModel = new SelectModel();
                selectModel.setHp_title(hp_title);
                selectModel.setHp_author(hp_author);
                selectModel.setHp_content(hp_content);
                selectModel.setLast_update_date(last_update_date);
                selectModel.setHp_img_url(hp_img_url);
                selectModel.setWeb_url(web_url);
                selectModel.setText_authors(text_authors);
                mList.add(selectModel);

            }

            //创建分页
            for (int i = 0; i < mList.size(); i++) {
                View view = View.inflate(getActivity(), R.layout.layout_select_pager_item, null);

                //init
                ImageView iv_img = view.findViewById(R.id.iv_img);
                TextView tv_title = view.findViewById(R.id.tv_title);
                TextView tv_author = view.findViewById(R.id.tv_author);
                TextView tv_content = view.findViewById(R.id.tv_content);
                TextView tv_last_update_date = view.findViewById(R.id.tv_last_update_date);

                SelectModel model = mList.get(i);
                GlideUtils.loadImageView(getActivity(),model.getHp_img_url(),iv_img);
                tv_title.setText(model.getHp_title());
                tv_author.setText(model.getHp_author());
                tv_content.setText(model.getHp_content());
                tv_last_update_date.setText(model.getLast_update_date());

                mListPagerView.add(view);

                //跳转浏览器
                iv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getActivity(), WebViewActivity.class);
                        intent.putExtra("title",model.getText_authors());
                        intent.putExtra("url",model.getWeb_url());
                        startActivity(intent);
                    }
                });
            }

            //预加载
            mSelectViewpager.setOffscreenPageLimit(mList.size());
            mSelectViewpager.setAdapter(new ViewPagerAdapter(mListPagerView));
            setCount(1,mListPagerView.size());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //监听下标
    private void setCount(int now,int all){
        tv_now_count.setText(now+"");
        tv_all_count.setText(all+"");
    }
}
